package com.bot.edu.informationmanage.stu.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.bot.edu.applysystem.applyinfo.entity.ApplyInfo;
import com.bot.edu.applysystem.applyinfo.service.IApplyInfoService;
import com.bot.edu.employmentsystem.employmen.entity.EmploymenInformation;
import com.bot.edu.employmentsystem.employmen.service.IEmploymenInformationService;
import com.bot.edu.informationmanage.stu.entity.Stu;
import com.bot.edu.informationmanage.stu.service.IStuService;
import com.bot.edu.informationmanage.stumiddlecls.entity.StuMiddleCls;
import com.bot.edu.informationmanage.stumiddlecls.service.IStuMiddleClsService;
import com.bot.edu.informationmanage.users.entity.UserInfo;
import com.bot.edu.informationmanage.users.service.IUserInfoService;
import com.bot.edu.utils.SnowFlake;
import com.bot.edu.utils.ToNotices;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 学生信息表
 * @Author: jeecg-boot
 * @Date:   2022-03-31
 * @Version: V1.0
 */
@Api(tags="学生信息表")
@RestController
@RequestMapping("/stu/stu")
@Slf4j
public class StuController extends JeecgController<Stu, IStuService> {
     @Autowired
     private IStuService stuService;

     @Autowired
     private IEmploymenInformationService employmenInformationService;

     @Autowired

     private IUserInfoService iUserInfoService;

     @Autowired

     private IApplyInfoService iApplyInfoService;

     @Autowired
     private IStuMiddleClsService stuMiddleClsService;

     /**
      * 分页列表查询
      *
      * @param stu
      * @param pageNo
      * @param pageSize
      * @param req
      * @return
      */
     //@AutoLog(value = "学生信息表-分页列表查询")
     @ApiOperation(value = "学生信息表-分页列表查询", notes = "学生信息表-分页列表查询")
     @GetMapping(value = "/list")
     public Result<IPage<Stu>> queryPageList(Stu stu,
                                             @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                             @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                             HttpServletRequest req) {
         QueryWrapper<Stu> queryWrapper = QueryGenerator.initQueryWrapper(stu, req.getParameterMap());
         Page<Stu> page = new Page<Stu>(pageNo, pageSize);
         IPage<Stu> pageList = stuService.page(page, queryWrapper);
         return Result.OK(pageList);
     }

     /**
      * 添加
      *
      * @param stu
      * @return
      */
     @AutoLog(value = "学生信息表-添加")
     @ApiOperation(value = "学生信息表-添加", notes = "学生信息表-添加")
     @PostMapping(value = "/add")
     public Result<String> add(@RequestBody Stu stu) {
         //用雪花算法生成学生编号
         String id = SnowFlake.getId();
         //给学生编号赋值
         stu.setStuId(id);
         stuService.save(stu);
         return Result.OK("添加成功！");
     }

     /**
      * 编辑
      *
      * @param stu
      * @return
      */
     @AutoLog(value = "学生信息表-编辑")
     @ApiOperation(value = "学生信息表-编辑", notes = "学生信息表-编辑")
     @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
     public Result<String> edit(@RequestBody Stu stu) {
         stuService.updateById(stu);
         return Result.OK("编辑成功!");
     }

     /**
      * 通过id删除
      *
      * @param id
      * @return
      */
     @AutoLog(value = "学生信息表-通过id删除")
     @ApiOperation(value = "学生信息表-通过id删除", notes = "学生信息表-通过id删除")
     @DeleteMapping(value = "/delete")
     public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
         stuService.removeById(id);
         return Result.OK("删除成功!");
     }

     /**
      * 批量删除
      *
      * @param ids
      * @return
      */
     @AutoLog(value = "学生信息表-批量删除")
     @ApiOperation(value = "学生信息表-批量删除", notes = "学生信息表-批量删除")
     @DeleteMapping(value = "/deleteBatch")
     public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
         this.stuService.removeByIds(Arrays.asList(ids.split(",")));
         return Result.OK("批量删除成功!");
     }

     /**
      * 通过id查询
      *
      * @param id
      * @return
      */
     //@AutoLog(value = "学生信息表-通过id查询")
     @ApiOperation(value = "学生信息表-通过id查询", notes = "学生信息表-通过id查询")
     @GetMapping(value = "/queryById")
     public Result<Stu> queryById(@RequestParam(name = "id", required = true) String id) {
         Stu stu = stuService.getById(id);
         if (stu == null) {
             return Result.error("未找到对应数据", null);
         }
         return Result.OK(stu);
     }

     /**
      * 导出excel
      *
      * @param request
      * @param stu
      */
     @RequestMapping(value = "/exportXls")
     public ModelAndView exportXls(HttpServletRequest request, Stu stu) {
         return super.exportXls(request, stu, Stu.class, "学生信息表");
     }

     /**
      * 通过excel导入数据
      *
      * @param request
      * @param response
      * @return
      */
     @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
     public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
         return super.importExcel(request, response, Stu.class);
     }

     /**
      * 修改学生状态
      */
     @GetMapping(value = "/upStatus/{id}/{newStuStatus}")
     public Result<?> upstatusById(@PathVariable("id") String id,
                                   @PathVariable("newStuStatus") String newStuStatus) {
         Stu stu = new Stu();
         stu.setStuStatusId(newStuStatus);
         QueryWrapper<Stu> queryWrapper = new QueryWrapper<>();
         queryWrapper.lambda().eq(Stu::getId, id);
         stuService.update(stu, queryWrapper);
         return Result.OK("修改成功！！");
     }

     /**
      * - 学生集合
      * - @return
      */
     @RequestMapping(value = "/getStuList", method = RequestMethod.GET)
     public Result<?> getStuList() {
         List<Stu> stuList = stuService.list();
         return Result.ok(stuList);
     }


     /**
      * 回显学生对应的就业信息
      */
     @PostMapping(value = "/selectEmploymenInformationByStuId")
     public Result<EmploymenInformation> selectEmploymenInformationByStuId(@RequestParam("stuId") String stuId) {

         QueryWrapper<EmploymenInformation> employmenInformationQueryWrapper = new QueryWrapper<>();
         employmenInformationQueryWrapper.lambda().eq(EmploymenInformation::getStuId, stuId);

         EmploymenInformation employmenInformation = employmenInformationService.getOne(employmenInformationQueryWrapper);
         return Result.OK(employmenInformation);
     }

     /**
      * 回显学生对应的就业维护信息
      *//*
     @PostMapping(value = "/selectEmploymenMaintainByStuId")
     public Result<EmploymenMaintain> selectEmploymenMaintainByStuId(@RequestParam("stuId") String stuId){
         QueryWrapper<EmploymenInformation> employmenInformationQueryWrapper = new QueryWrapper<>();
         employmenInformationQueryWrapper.lambda().eq(EmploymenInformation::getStuId,stuId);
         EmploymenInformation employmenInformation = employmenMaintainService.getOne(employmenInformationQueryWrapper);
         return Result.OK(employmenInformation);
     }*/

     /**
      * - 报名成功后生成学生信息
      */
     @AutoLog(value = "学生信息表-生成")
     @ApiOperation(value = "学生信息表-生成", notes = "学生信息表-生成")
     @PostMapping(value = "/autoAddStu/{applyId}/{userId}")
     public Result<String> autoAddStu(@PathVariable String applyId, @PathVariable String userId) {
         //根据用户编号查询用户
         QueryWrapper<UserInfo> userInfoQueryWrapper = new QueryWrapper<>();
         userInfoQueryWrapper.lambda().eq(UserInfo::getUserId, userId);
         UserInfo userInfo = iUserInfoService.getOne(userInfoQueryWrapper);

         //根据报名编号查询报名信息
         QueryWrapper<ApplyInfo> applyInfoQueryWrapper = new QueryWrapper<>();
         applyInfoQueryWrapper.lambda().eq(ApplyInfo::getApplyId, applyId);
         ApplyInfo applyInfo = iApplyInfoService.getOne(applyInfoQueryWrapper);

         //生成一条学生信息
         Stu stu = new Stu();
         //将用户编号赋给学生编号
         stu.setStuId(userId);
         stu.setStuAccount(userInfo.getUserUname());
         stu.setStuPassword(userInfo.getUserPwd());
         stu.setStuUsername(userInfo.getUserUname());
         stu.setStuPhoto(userInfo.getUserPhoto());
         stu.setStuName(applyInfo.getApplyName());
         stu.setStuAge(userInfo.getUserAge());
         stu.setAddressId(applyInfo.getApplyAddress());
         stu.setStuPhone(applyInfo.getApplyPhone());
         stu.setEducationId(applyInfo.getEducationId());
         stu.setMajorId(null);
         stu.setStuIdcard(applyInfo.getApplyIdcard());
         stu.setStuSex(applyInfo.getApplySex());
         stuService.save(stu);
         //生成默认为空的就业信息
         EmploymenInformation employmenInformation = new EmploymenInformation();
         employmenInformation.setStuId(userId);
         employmenInformation.setCreateBy("平台");
         employmenInformation.setCreateTime(new Date());
         employmenInformationService.save(employmenInformation);
         /*生成默认无班级状态*/
         StuMiddleCls stuMiddleCls = new StuMiddleCls();
         stuMiddleCls.setId(SnowFlake.getId());
         stuMiddleCls.setStuId(stu.getStuId());
         stuMiddleCls.setClassId("0");
         stuMiddleClsService.save(stuMiddleCls);
         return Result.OK("添加成功！");
     }


     /**
      * app登陆
      * @param phoneData
      * @param passData
      * @return
      */
     @GetMapping("/appLogin")
     public Result<?> appLogin(@RequestParam("phoneData") String phoneData, @RequestParam("passData") String passData, HttpSession session){
         QueryWrapper<Stu> stuQueryWrapper = new QueryWrapper<>();
         stuQueryWrapper.lambda().eq(Stu::getStuPhone,phoneData).eq(Stu::getStuPassword,passData);
         Stu stu = stuService.getOne(stuQueryWrapper);
         QueryWrapper<UserInfo> userInfoQueryWrapper = new QueryWrapper<>();
         userInfoQueryWrapper.lambda().eq(UserInfo::getUserPhone,phoneData).eq(UserInfo::getUserPwd,passData);
         UserInfo userInfo = iUserInfoService.getOne(userInfoQueryWrapper);
         ToNotices.userId = userInfo.getUserId();
         if (stu==null){
             if (userInfo==null){
                 return Result.ok(userInfo);
             }else {
                 return Result.OK("用户登陆",userInfo);

             }
         }else {
             ArrayList<Object> list = new ArrayList<>();
             list.add(0,userInfo);
             list.add(1,stu);
             return Result.OK("学生登陆",list);
         }
     }
 }